package aceona.api.weibo.sina.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import aceona.api.weibo.sina.Parser;
import aceona.api.weibo.sina.SinaScannerConsts;
import aceona.api.weibo.sina.Tools;
import aceona.api.weibo.sina.po.Baseinfo;
import aceona.api.weibo.sina.po.Education;
import aceona.api.weibo.sina.po.WorkExperience;

public class SinaDocParser implements Parser {
	private static Document doc;
	
	@Override
	public String urlParser(String selector) {
		Elements elements = doc.select(selector);
		if(elements.get(0).hasAttr("href")){
			return elements.get(0).attr("href");
		}else if(elements.get(0).hasAttr("src")){
			return elements.get(0).attr("src");
		}
		return "";
	}
	
	@Override
	public String urlParser(String selector,String containText) {
		Elements elements = doc.select(selector);
		Iterator<Element> it = elements.iterator();
		while(it.hasNext()){
			Element e = it.next();
			if(e.text().indexOf(containText) > -1){
				return e.hasAttr("href")?e.attr("href"):"";
			}
		}
		return "";
	}
	
	public void baseInfoParser() {
		String head_image_url = "";//基本资料----头像url
		head_image_url = urlParser(SinaScannerConsts.getProperty("sina.weibo.baseinfo.headimage.url"));
		Elements tips = doc.select(SinaScannerConsts.getProperty("sina.weibo.baseinfo.tips"));
		Iterator<Element> iterator = tips.iterator();
		Baseinfo baseinfo = new Baseinfo();
		baseinfo.setHeadImageUrl(head_image_url);
		while(iterator.hasNext()){
			Element e = iterator.next();
			if(e.text().indexOf("基本信息") > -1){
				Element c = e.nextElementSibling();//获取到基本信息区块
				String matcherStr = c.text();
				if(baseinfo == null){
					baseinfo = new Baseinfo();
				}
				baseinfo.setNickName(Tools.getResult("昵称:([^\\s]+)\\s", matcherStr));
				baseinfo.setBirthday(Tools.getResult("生日:([^\\s]+)\\s", matcherStr));
				baseinfo.setArea(Tools.getResult("地区:([^\\s]+)\\s", matcherStr));
				baseinfo.setIntro(Tools.getResult("简介:([^\\s]+)\\s", matcherStr));
				baseinfo.setSex(Tools.getResult("性别:([^\\s]+)\\s", matcherStr));
			}else if(e.text().indexOf("学习经历") > -1){
				Element c = e.nextElementSibling();//获取到学习经历区块
				String matcherStr = c.html();
				List<Education> list = null;
				String[] schoollist = matcherStr.split("<br />");
				if(schoollist != null && schoollist.length > 0){
					list = new ArrayList<Education>();
					for(int i = 0; i < schoollist.length; i++){
						Education edu = new Education();
						edu.setSchoolName(schoollist[i].replace("\n", "").substring(schoollist[i].indexOf("&middot;")+8,schoollist[i].indexOf("&nbsp;")));
						edu.setJoinTime(schoollist[i].replace("\n", "").substring(schoollist[i].indexOf("&nbsp;")+6));
						list.add(edu);
					}
				}
				baseinfo.setEduction(list);
//				System.out.println(Tools.getResult("(\\d{2})级", matcherStr));
			}else if(e.text().indexOf("工作经历") > -1){
				Element c = e.nextElementSibling();//获取到工作经历区块
				String matcherStr = c.html();
				List<WorkExperience> list = null;
				String[] worklist = matcherStr.split("<br />");
				if(worklist != null && worklist.length > 0){
					list = new ArrayList<WorkExperience>();
					for(int i = 0; i < worklist.length; i++){
						WorkExperience edu = new WorkExperience();
						edu.setCorpName(worklist[i].replace("\n", "").substring(worklist[i].indexOf("&middot;")+8,worklist[i].indexOf("&nbsp;")));
						edu.setJoinTime(worklist[i].replace("\n", "").substring(worklist[i].indexOf("&nbsp;")+6));
						list.add(edu);
					}
				}
				baseinfo.setWorkExperience(list);
			}
		}
		
		
		ObjectMapper mapper = new ObjectMapper();
		try {
			System.out.println(mapper.writeValueAsString(baseinfo));
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
//		System.out.println("查找到的tips块数:"+tips.size());
//		System.out.println("头像Url:   "+head_image_url);
//		System.out.println(doc.toString());
	}
	
	public void setDocument(Document document){
		doc = document;
	}
}
